﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Surpass.Repository.Repository ;
using Surpass.Model;
using Microsoft.AspNetCore.Authorization;
using Surpass.Repository.IRepository;
using Surpass.Common.Authorize;
using Surpass.Model.Dto.RequestParamsDto;

namespace Surpass.Admin.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class LoginController : ControllerBase
    {
        private readonly ISysUserRepository _sysUserDao;
        public LoginController(ISysUserRepository sysUserDao)
        {
            _sysUserDao = sysUserDao;
        }
        [HttpPost,Route("Login"), AllowAnonymous]
        
        public async Task<IActionResult> Login(LoginParams loginParams)
        {
            var user = await _sysUserDao.Login(loginParams);
            if (user.Success)
            {
                
                var tokenstr = TokenManager.GenerateToken(loginParams.LoginEMail);
                //将token 写入请求差头
                //HttpContext.Response.OnStarting(state => {
                //    HttpContext.Response.Cookies.Append("qmtoken", tokenstr);

                //    return Task.FromResult(0);
                //}, HttpContext);
                HttpContext.Response.Headers["token"] = tokenstr;
                HttpContext.Response.Headers["Access-Control-Expose-Headers"] = "token";

                return Ok(user);
            }
            else
            {
                return Ok(user);
            }
            
            //_sysUserDao.Login();
        }
        [HttpGet]
        
        public IActionResult Show()
        {
            return Ok("abc");
        }
    }
}
